package com.liantong.service.util;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.liantong.util.StringUtil;


/**
 * @author ZZF on 2012-5-14
 */
public class FileUtil {
	public static String getRealPath(String resPath) {

		if (!StringUtil.isBlank(resPath)) {

//			if (resPath.startsWith("/apps")) {

				return Constant.APP_PATH + resPath;
//			}

			//return resPath;

		}
		return "";
	}

	public static String getDestinationFilePath(String appId, String fileMd5, String fileName) {

		String result = getPathFromDate() + appId + "_" + fileMd5 + getFileType(fileName);

		return result;
	}

	public static String getFileType(String fileName) {

		String[] splits = fileName.split("\\.");

		if (splits.length > 1) {
			return "." + splits[splits.length - 1];
		}

		return "";
	}

	public static String getPathFromDate() {

		SimpleDateFormat simpleFormat = new SimpleDateFormat("/yyyy/MM/");
		String result = simpleFormat.format(new Date(System.currentTimeMillis()));

		return result;
	}

	public static void delete(String filePath) {
		File file = new File(filePath);

		if (file.exists()) {
			file.delete();
		}
	}

	public static void delete(File deleteFile) {

		if (deleteFile.exists()) {
			deleteFile.delete();
		}
	}

	public static boolean exist(File file) {

		if (file.exists() && file.isFile()) {
			return true;
		}
		return false;
	}

}
